package cn.hy.batch.reader;

import cn.hy.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.MultiResourceItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.validation.BindException;

import java.util.List;

@Configuration
@Slf4j
public class ItemReaderMultFilerDemo {

    //注入任务对象工厂
    @javax.annotation.Resource
    private JobBuilderFactory jobBuilderFactory;

    //任务的执行由Step决定,注入step对象的factory
    @javax.annotation.Resource
    private StepBuilderFactory stepBuilderFactory;

    @Value("classpath:/user_*.txt") //注入文件路径user_1.txt,user_2.txt
    private Resource[] resources;

    //创建Job对象
    @Bean
    public Job jobDemoMultfile() {
        return jobBuilderFactory.get("jobDemoMultfile")
                .start(step_multfile())
                .build();
    }

    //创建Step对象
    @Bean
    public Step step_multfile() {
        return stepBuilderFactory.get("step_multfile")
                .<User, User>chunk(2)
                .reader(multiFileReader()) //指定文件读取器
                .writer(new ItemWriter<User>() {
                    @Override
                    public void write(List<? extends User> list) throws Exception {
                        System.out.println("输出list");
                        for (User user : list) {
                            System.out.println(user);
                        }
                    }
                })
                .build();
    }

    @Bean
    public MultiResourceItemReader<User> multiFileReader() { //聚合的读取器
        MultiResourceItemReader<User> reader = new MultiResourceItemReader<>();
        reader.setDelegate(fileReader02()); //指定单个文件的读取方法
        reader.setResources(resources); //指定资源，需要被读取的文件
        return reader;
    }

    @Bean
    @StepScope
    public FlatFileItemReader<User> fileReader02() {
        FlatFileItemReader<User> reader = new FlatFileItemReader<>();
        reader.setLinesToSkip(1);

        //数据解析
        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        tokenizer.setNames("id", "name");//按照位置与文件中的字段对应

        //把解析的数据映射为对象
        DefaultLineMapper<User> mapper = new DefaultLineMapper<>();
        mapper.setLineTokenizer(tokenizer);
        mapper.setFieldSetMapper(new FieldSetMapper<User>() {
            @Override
            public User mapFieldSet(FieldSet fieldSet) throws BindException {
                User user = new User();
                user.setId(fieldSet.readInt("id"));
                user.setName(fieldSet.readString("name"));
                return user;
            }
        });
        mapper.afterPropertiesSet();
        reader.setLineMapper(mapper);//行转换器
        return reader;
    }

}
